<?php

namespace Addons\Bdjw\Controller;

use Home\Controller\AddonsController;
use Org\Util\Snoopy;
use Think\Verify;

class BdjwController extends AddonsController
{

    public function index()
    {
        $this->display();
    }

    public function verify()
    {
        $config = array(
            'length' => 4, // 验证码位数
            'fontSize' => 40,
            'useCurve' => false,
            'reset' => false, // 验证成功后是否重置
        );
        $Verify = new Verify($config);
        $Verify->entry();
    }

    public function login()
    {
        if (check_verify(I("post.verify"), "")) {
            $userinfo = M("userinfo");
            $data["openid"] = get_openid();
            $data["token"] = get_token();
            if ($userinfo->where($data)->select() > 0) {
                redirect(addons_url('Gbck://Gbck/cfbd'));
            } else {
                $post['token'] = "wxfwh";
                $post["wxopenid"] = get_openid();
                $post['xh'] = I("post.xh");
                $post['passwordtext'] = I("post.password");

                $sn = new Snoopy();
                $url = "http://lvc.gxsdxy.cn/api/JwStuCA/JwStuCA01";
                $sn->submit($url, $post);
                if ($sn->results == 1) {
                    $data["xh"] = $post['xh'];
                    $data["openid"] = get_openid();
                    $data["token"] = get_token();
                    if ($userinfo->add($data)) {
                        redirect(addons_url('Gbck://Gbck/index'));
                    } else {
                        $this->error("绑定失败，请重新绑定:(", addons_url('Bdjw://Bdjw/index'));

                    }
                } else {
                    $this->error("你的账号密码有误，或者不存在:(", addons_url('Bdjw://Bdjw/index'));
                }
            }
        } else {
            $this->error("验证码不正确:(", addons_url('Bdjw://Bdjw/index'));
        }

    }


    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify()
    {
        $code = I("post.verify");
        $id = '';
        $verify = new Verify();
//        return $verify->check($code, $id);
        $this->ajaxReturn($verify->check($code, $id));
    }
}
